package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.MdMedDmtbService;
import com.dw.etl.core.api.MdMedTbFldService;
import com.dw.etl.core.bean.model.MdMedDim;
import com.dw.etl.core.bean.model.MdMedDmtb;
import com.dw.etl.core.bean.model.MdMedTbFld;
import com.dw.etl.core.bean.vo.LayMsg;
import com.dw.etl.core.bean.vo.MdMedDmtbVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * mdMedDmtb控制器
 * Created by hozeData on 2021/07/13.
 */
@RestController
@RequestMapping("/etl/mdMedDmtb/")
@Api(tags="DM表管理")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MdMedDmtbController {

    @Autowired
    MdMedDmtbService mdMedDmtbService;
    @Autowired
    MdMedTbFldService medTbFldService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.GET)
    public LayMsg findByPage(String filter, Integer page, Integer limit) {
        PaginationVO<MdMedDmtb> pageVO = mdMedDmtbService.findPageByLike(filter, (page-1)*limit, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findIndex", method=RequestMethod.GET)
    public LayMsg findIndex() {
        List<Map<String,Object>> pageVO = mdMedDmtbService.findAllIndex();
        return LayMsg.success().addData(pageVO);
    }

    @ApiOperation(value="根据指标查询维度")
    @RequestMapping(value="findDimFieldByIdIndex", method=RequestMethod.GET)
    public LayMsg findDimFieldByIdIndex(String idIndex) {
        MdMedDmtb dmtb = mdMedDmtbService.getTbByIndex(idIndex);
        List<MdMedTbFld> list = medTbFldService.findTbDimField(dmtb.getIdTb());
        return LayMsg.success().addData(list);
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="mdMedDmtb", value="实体", paramType="body", required=true, dataType="MdMedDmtb")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public LayMsg save(@RequestBody MdMedDmtbVO mdMedDmtb, HttpServletRequest request)  {
        return mdMedDmtbService.save(mdMedDmtb);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idDmtb", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(String idDmtb, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDmtb)){
            throw new BaseException("请选择要编辑的项");
        }
        MdMedDmtb mdMedDmtb = mdMedDmtbService.getById(idDmtb);
        return new BaseResult(CommonResultConstant.SUCCESS, mdMedDmtb);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="mdMedDmtb", value="实体", paramType="body", required=true, dataType="MdMedDmtb")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody MdMedDmtb mdMedDmtb, HttpServletRequest request)  {
        int num = mdMedDmtbService.updateByIdSelective(mdMedDmtb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idDmtb", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(String idDmtb, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idDmtb)){
            throw new BaseException("请选择要删除的项");
        }
        int num = mdMedDmtbService.deleteById(idDmtb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
